﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Flame.Controls
{
    public class Cell : Flame.Dlr.Script
    {
        public bool Auto { get; set; }

        public Cell(Type language, string code, bool auto)
            : base(language, code)
        {
           Auto = auto ;
        }
        public Cell(Flame.Dlr.ScriptBrute s)
            : base(s)
        {
        }

        protected override void fromParameters(string[] parameters)
        {
            Auto = false;
            if (parameters.Length == 2)
            {
                if (parameters[1] != "a") throw new Flame.Dlr.ScriptException();

                Auto = true;
            }
            else if (parameters.Length != 1)
            {
                throw new Flame.Dlr.ScriptException();
            }

        }

        public override Flame.Dlr.ScriptBrute Brute
        {
            get
            {
                var basecell = base.Brute;
                basecell.Header = basecell.Header + (Auto ? ";a" : "");
                return basecell;
            }
        }

        public override bool Equals(object cell)
        {
            return Auto == ((Cell)cell).Auto && (Code == ((Cell)cell).Code || Code == null || ((Cell)cell).Code == null) && (Language == ((Cell)cell).Language || Language == Flame.Dlr.Languages.None || ((Cell)cell).Language == Flame.Dlr.Languages.None);
        }

        public override int GetHashCode() { return base.GetHashCode(); }
    }

    /*public class Cell : Flame.Dlr.Cell
    {
        public Cell() { }
        public Cell(Flame.Dlr.CellHeader x)
        {
            Code = x.Code;
            string[] parameters = x.Header.Split(';');
            if (parameters.Length == 1)
                Language = Flame.Dlr.Manager.TryFromName(parameters[0]);
            else if (parameters.Length == 2)
            {
                if (parameters[1] != ";a") throw new Flame.Dlr.ScriptException();

                Language = Flame.Dlr.Manager.TryFromName(parameters[0]);

                Auto = true;
            }
            else
            {
                throw new Flame.Dlr.ScriptException();
            }
            /* 
            // temp
            string lang = Language.ToLower();
            if (lang == "python" || lang == "py")
                Language = "IronPython";
            else if (lang == "ruby" || lang == "rb")
                Language = "IronRuby"; * /
        }
        public bool Auto { get; set; }

        public override bool Equals(object cell)
        {
            return Auto == ((Cell)cell).Auto && (Code == ((Cell)cell).Code || Code == null || ((Cell)cell).Code == null) && (Language == ((Cell)cell).Language || Language == Flame.Dlr.Languages.Get("None") || ((Cell)cell).Language == Flame.Dlr.Languages.Get("None"));
        }

        public override int GetHashCode() { return base.GetHashCode(); }

        public override Flame.Dlr.CellHeader Base
        {
            get
            {
                var basecell = base.Base;
                basecell.Header = basecell.Header + (Auto ? ";a" : "");
                return basecell;
            }
        }
    }*/
}
